package com.jxs.android.sxyw;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.CaseInfo;
import com.jxs.android.sxyw.data.OrderType;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.RefreshListView;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StringUtils;

/**
 * @Des EOMS 告警
 * @Author jiangxs
 * @Des 2012-8-20 下午2:03:35
 */
public abstract class CaseList extends AbstractListActivity {

	private static final int UPDATE_LIST_UI = 0x101;
	private static final int ERROR_MSG = 0x1013;
	private static final int TO_LOGIN = 0x109;

	private String ticket = "";

	private RefreshListView refreshListView;
	private TextView empytText;
	private CaseAdapter caseInfoAdapter;

	private int currentPage = 1; // 最后的可视项索引

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.common_with_list);
		if (null == SelfApplication.getUserInfo() || StringUtils.isEmpety(SelfApplication.getUserInfo().getTicket())) {
			PopMsg.show("登录已过期,请重新登录");
			StartHelper.startActivity(Login.class);
			return;
		}

		refreshListView = (RefreshListView) findViewById(R.id.common_listview);
		empytText = (TextView) findViewById(R.id.null_has_data);
		empytText.setText(empytTip());
		caseInfoAdapter = new CaseAdapter(CaseList.this);
		refreshListView.setAdapter(caseInfoAdapter);
		ticket = SelfApplication.getUserInfo().getTicket();

		loadingData(currentPage, true);
		refreshListView.setonRefreshListener(new RefreshListView.OnRefreshListener() {
			@Override
			public void onRefresh() {
				loadingData(currentPage, false);
			}
		});
	}

	private void loadingData(int currentPage, boolean isShow) {
		CaseList.this.getWebServiceData(ticket, currentPage, getWhich(), isShow, new DataInitListeren() {
			@Override
			public void initData(ProgressDialog progressDialog, boolean isShow) {
				if (isShow) {
					progressDialog.dismiss();
				}
			}
		});
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.setTitle(title());
		initTopBarReturn(topBar);
		topBar.hindBtn();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void paser(int pageNo, SoapObject soapObject) {
		if (null != soapObject) {
			String stauts = soapObject.getPropertySafelyAsString("status");
			SoapObject msgObjec = (SoapObject) soapObject.getProperty("msg");
			String message = msgObjec.getPropertySafelyAsString("message");
			if ("-2".equals(stauts)) {
				Message msg = new Message();
				msg.what = ERROR_MSG;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("-1".equals(stauts)) {
				Message msg = new Message();
				msg.what = TO_LOGIN;
				msg.obj = message;
				handler.sendMessage(msg);
			} else if ("0".equals(stauts)) {
				SoapObject retMsgObject = (SoapObject) soapObject.getProperty("retMsg");
				List<CaseInfo> taskInfos = new ArrayList<CaseInfo>();
				if (null != retMsgObject) {
					for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
						SoapObject emosBeanObject = (SoapObject) retMsgObject.getProperty(i);

						CaseInfo caseInfo = new CaseInfo();
						caseInfo.setOrder_id(emosBeanObject.getPropertySafelyAsString("order_id"));
						caseInfo.setCase_no(emosBeanObject.getPropertySafelyAsString("case_no"));
						caseInfo.setTitle(emosBeanObject.getPropertySafelyAsString("title"));
						caseInfo.setDescription(emosBeanObject.getPropertySafelyAsString("description"));
						caseInfo.setStatus(emosBeanObject.getPropertySafelyAsString("status"));
						caseInfo.setStatus_name(emosBeanObject.getPropertySafelyAsString("status_name"));
						caseInfo.setOccur_time(emosBeanObject.getPropertySafelyAsString("occur_time"));
						caseInfo.setPlan_finish_time(emosBeanObject.getPropertySafelyAsString("plan_finish_time"));
						caseInfo.setResume_time(emosBeanObject.getPropertySafelyAsString("resume_time"));
						caseInfo.setCarrier_id(emosBeanObject.getPropertySafelyAsString("carrier_id"));
						caseInfo.setCarrier_name(emosBeanObject.getPropertySafelyAsString("carrier_name"));
						caseInfo.setCompany_id(emosBeanObject.getPropertySafelyAsString("company_id"));
						caseInfo.setCompany_name(emosBeanObject.getPropertySafelyAsString("company_name"));
						caseInfo.setSpecialty_id(emosBeanObject.getPropertySafelyAsString("specialty_id"));
						caseInfo.setSpecialty_name(emosBeanObject.getPropertySafelyAsString("specialty_name"));
						caseInfo.setBusiness_id(emosBeanObject.getPropertySafelyAsString("business_id"));
						caseInfo.setBusiness_name(emosBeanObject.getPropertySafelyAsString("business_name"));
						caseInfo.setOrderType(OrderType.get(Integer.valueOf(emosBeanObject.getPropertySafelyAsString("order_type"))));

						taskInfos.add(caseInfo);
					}
				}
				Message msg = new Message();
				msg.what = UPDATE_LIST_UI;
				msg.obj = taskInfos;
				handler.sendMessage(msg);
			}
		}
	}

	Handler handler = new Handler() {

		@Override
		@SuppressWarnings("unchecked")
		public void handleMessage(Message msg) {
			if (msg.what == UPDATE_LIST_UI) {

				List<CaseInfo> tems = (List<CaseInfo>) msg.obj;
				empytText.setVisibility(View.GONE);
				caseInfoAdapter.addCaseInfos(tems);
				caseInfoAdapter.notifyDataSetChanged();

				if (currentPage == 1) {
					if (tems.size() < 1) {
						empytText.setVisibility(View.VISIBLE);
					}
				}
				refreshListView.onRefreshComplete();
			} else if (msg.what == ERROR_MSG) {
				PopMsg.show((String) msg.obj);
			} else if (msg.what == TO_LOGIN) {
				PopMsg.show((String) msg.obj);
				StartHelper.startActivity(Login.class);
			}
		}

	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == CaseDetail.GET_DETAIL && resultCode == RESULT_OK) {
			caseInfoAdapter.getCaseInfos().remove(data.getExtras().getInt("position"));
			caseInfoAdapter.notifyDataSetChanged();
			if (caseInfoAdapter.getCaseInfos().size() < 1) {
				empytText.setVisibility(View.VISIBLE);
			}
		}

	}

	protected abstract String title();

	protected abstract Which getWhich();

	private String empytTip() {
		return "暂无" + title() + "信息";
	}

	@Override
	public String getMethod() {
		return "getWorkOrderPage";
	}

}
